package com.symbio.ishenapi.laboratory.controller;

import com.symbio.ishenapi.common.controller.BaseController;
import com.symbio.ishenapi.laboratory.bean.*;
import com.symbio.ishenapi.laboratory.model.TmLaboratoryBasis;
import com.symbio.ishenapi.laboratory.model.TmLaboratoryIndicators;
import com.symbio.ishenapi.laboratory.model.TmLaboratoryInfo;
import com.symbio.ishenapi.laboratory.model.TmLaboratoryInfoAttr;
import io.jboot.web.controller.annotation.RequestMapping;

import java.math.BigInteger;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName TmLaboratoryController
 * @Description 化验单模块 controller
 * @Author GuoShunYou
 * @Date 2018/10/25  上午 10:10
 **/

@RequestMapping("/TmLaboratoryController")
public class TmLaboratoryController extends BaseController {

    /**
     * @Author GuoShunYou
     * @Description 查询化验单种类列表
     * @date 上午 10:46   2018/10/25
     * @return void
     */
    public void selectBasis(){
        List<TmLaboratoryBasis> list=TmLaboratoryBasis.dao.selectBasis();
        renderJsonRet(list);
    }

    /**
     * @Author GuoShunYou
     * @Description 根据化验单id查询所关联指标
     * @date 上午 11:31   2018/10/25
     * @return void
     */
    public void selectIndicatorsByBasisId(){
        TmLaboratoryIndicators toc=(TmLaboratoryIndicators)getReqBodyObject(TmLaboratoryIndicators.class);
        List<TmLaboratoryIndicators> list=TmLaboratoryIndicators.dao.selectInById(toc);
        renderJsonRet(list);
    }

    /**
     * @Author GuoShunYou
     * @Description 查询所有排序指标
     * @date 下午 3:51   2018/10/26
     * @return void
     */
    public void selectBySeq(){
        TmLaboratoryIndicators toc=(TmLaboratoryIndicators)getReqBodyObject(TmLaboratoryIndicators.class);
        List<TmLaboratoryIndicators> list=TmLaboratoryIndicators.dao.seleBySeq();
        renderJsonRet(list);
    }

    /**
     * @Author GuoShunYou
     * @Description 添加  1-图片化验单
     * @date 下午 3:28   2018/11/2
     * @return void
     */
    public  void addPahtLaboratory(){
        LaboratoryInfoList toc=(LaboratoryInfoList)getReqBodyObject(LaboratoryInfoList.class);
        SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
        int count=0;
        for(int i=0;i<toc.getLaboratoryPath().size();i++){
            TmLaboratoryInfo labainfo= new TmLaboratoryInfo();
            labainfo.setUserId(toc.getUserId());
            labainfo.setTime(toc.getTime());
            try {
                labainfo.setCheckTime(date.parse(toc.getCheckTime()));
            }catch (Exception e){

            }

            labainfo.setCreatePeople(toc.getCreatePeople());
            labainfo.setCreateid(toc.getCreateId());
            labainfo.setCreateTime(new Date());
            labainfo.setLaboratoryType("1");
            labainfo.setLaboratoryPath(toc.getLaboratoryPath().get(i));
            boolean b=labainfo.save();
            if(b){
                count++;
            }
        }

        if(count==toc.getLaboratoryPath().size()){
            renderJsonError(40001,"图片类型化验单");
        }else{
            renderJsonError(40002,"图片类型化验单");
        }
    }

    /**
     * @Author GuoShunYou
     * @Description 添加0 数据化验单
     * @date 下午 3:20   2018/10/25
     * @return void
     */
    public void addLaboratory(){
        LaboratoryIndicators toc=(LaboratoryIndicators)getReqBodyObject(LaboratoryIndicators.class);

        int num=TmLaboratoryInfo.dao.getCountLaboratory(toc);
        if(num>0){
            renderJsonError(40015,"该化验单");
            return;
        }



        //添加化验单记录主表数据
        TmLaboratoryInfo tminfo=new TmLaboratoryInfo();
        SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
        //ParsePosition pos = new ParsePosition(0);
        try {
            String time=toc.getCheckTime();
           /* System.out.println(time);
            System.out.println(date.parse(toc.getCheckTime(),pos));*/
            tminfo.setCheckTime(date.parse(time));
        }catch (ParseException e){
            e.printStackTrace();
        }
        tminfo.setLaboratoryId(toc.getLoboratoryId());
        tminfo.setCreateTime(new Date());
        tminfo.setLaboratoryPath(toc.getPath());
        tminfo.setLaboratoryType(toc.getLabortoryType());
        tminfo.setUserId(toc.getUserId());
        tminfo.setTime(toc.getTime());
        tminfo.save();

        //添加化验单指标配置数据表
        for(int i=0;i<toc.getList().size();i++){
            TmLaboratoryInfoAttr tmlinfoattr=new TmLaboratoryInfoAttr();
            tmlinfoattr.setLaboratoryInfoId(tminfo.getId());
            tmlinfoattr.setIndicatorsId(toc.getList().get(i).getIndicatorsId());
            tmlinfoattr.setIndicatorsResult(toc.getList().get(i).getIndicatorsResult());
            tmlinfoattr.save();
        }
        renderJsonError(40001,"化验单数据");
    }

    /**
     * @Author GuoShunYou
     * @Description 修改图片化验单
     * @date 上午 7:51   2018/11/4
     * @return void
     */
    public void updatePahtLabortory(){
        TmLaboratoryInfo toc=(TmLaboratoryInfo)getReqBodyObject(TmLaboratoryInfo.class);
        boolean b=toc.update();
        if(b){
            renderJsonError(40004,"图片化验单");
        }else{
            renderJsonError(40005,"图片化验单");
        }
    }

    /**
     *
     *
     * @Author GuoShunYou
     * @Description 修改数据化验单
     * @date 下午 4:12   2018/10/25
     * @return
     */
    public void updateLaboretory(){
        LaboratoryIndicators toc=(LaboratoryIndicators)getReqBodyObject(LaboratoryIndicators.class);
        //进行逻辑删除
        /*TmLaboratoryInfo tminfos=new TmLaboratoryInfo();
        tminfos.setId(toc.getId());
        tminfos.setStatus("0");
        tminfos.update();*/

        List<String> list=TmLaboratoryInfo.dao.getid(toc.getId());
        for(int i=0;i<list.size();i++){
            TmLaboratoryInfoAttr tliarr=new TmLaboratoryInfoAttr();
            tliarr.setId(new BigInteger(list.get(i)));
            tliarr.setStatus("0");
            tliarr.update();
        }

        //添加化验单记录主表数据
      /*  TmLaboratoryInfo tminfo=new TmLaboratoryInfo();
        SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
        try {
            String time=toc.getCheckTime();
            tminfo.setCheckTime(date.parse(time));
        }catch (ParseException e){
            e.printStackTrace();
        }
        tminfo.setLaboratoryId(toc.getLoboratoryId());
        tminfo.setCreateTime(new Date());
        tminfo.setLaboratoryPath(toc.getPath());
        tminfo.setLaboratoryType(toc.getLabortoryType());
        tminfo.setUserId(toc.getUserId());
        boolean b=tminfo.save();*/
        int count=0;

        //添加化验单指标配置数据表
        for(int i=0;i<toc.getList().size();i++){
            TmLaboratoryInfoAttr tmlinfoattr=new TmLaboratoryInfoAttr();
            tmlinfoattr.setLaboratoryInfoId(toc.getId());
            tmlinfoattr.setIndicatorsId(toc.getList().get(i).getIndicatorsId());
            tmlinfoattr.setIndicatorsResult(toc.getList().get(i).getIndicatorsResult());
            boolean bt=tmlinfoattr.save();
            if(bt){
                count++;
            }
        }

        if(count==toc.getList().size()){
            //进行物理删除
            // tminfos.delete();
            for(int i=0;i<list.size();i++){
                TmLaboratoryInfoAttr tliarr=new TmLaboratoryInfoAttr();
                tliarr.setId(new BigInteger(list.get(i)));
                tliarr.setStatus("0");
                tliarr.delete();
            }
        }
        renderJsonError(40004,"化验单数据");
    }

    /**
     * @Author GuoShunYou
     * @Description 根据userId、动态获取指标 查询对应化验单数据信息
     * @date 上午 9:37   2018/10/26
     * @return void
     */
    public void selectByIndicators(){
        InfoattrRequest toc=(InfoattrRequest)getReqBodyObject(InfoattrRequest.class);
        SimpleDateFormat sdfs = new SimpleDateFormat("yyyy-MM-dd");
        Format f = new SimpleDateFormat("yyyy-MM-dd");
        String indicaid=toc.getIndicatorsId();
        String[] indistr=indicaid.split(",");
        System.out.println("指标数组："+indistr);

        List<LaboratoryIndicatorsResponse> list=TmLaboratoryInfo.dao.seleByInfoattrId(toc);
        List<String> lsitchetime=TmLaboratoryInfo.dao.getChecktime(toc);
        System.out.println("日期分组："+lsitchetime.toString());
        List listall=new ArrayList<>();
        for(int i=0;i<indistr.length;i++){
            System.out.println("指标："+indistr[i]);
            //根据指标个数 创建不同指标集合
            List lsit=new ArrayList<>();
            String name="";
            for(int j=0;j<lsitchetime.size();j++){
                System.out.println("日期："+lsitchetime.get(j));
                for(int ij=0;ij<list.size();ij++){
                    if(list.get(ij).getCheckTime().equals(lsitchetime.get(j)) && indistr[i].equals(list.get(ij).getIndicatorsId())){
                        String res=list.get(ij).getIndicatorsResult();
                        name=list.get(ij).getIndicatorsname();
                        List litlist=new ArrayList<>();
                        litlist.add(0,lsitchetime.get(j));
                        litlist.add(1,res);
                        lsit.add(litlist);
                        try{
                            Date hou,qian;
                            if(j+1<lsitchetime.size()){
                                qian=sdfs.parse( lsitchetime.get(j));
                                hou=sdfs.parse( lsitchetime.get(j+1));
                                Long cha=(hou.getTime()-qian.getTime())/1000/60;
                                for( int k =0;k<cha/1440-1;k++){
                                    Calendar c = Calendar.getInstance();
                                    c.setTime(qian);
                                    c.add(Calendar.DAY_OF_MONTH,1);
                                    Date tom=c.getTime();
                                    qian=tom;
                                    List xiaolist=new ArrayList<>();
                                    xiaolist.add(0,f.format(qian));
                                    xiaolist.add(1,"0");
                                    lsit.add(xiaolist);
                                }
                            }
                        }catch (ParseException e){
                            e.printStackTrace();
                        }
                    }else{
                        name=TmLaboratoryIndicators.dao.getNmaeByid(indistr[i]);
                    }
                }
            }
            Map map=new HashMap<>();
            map.put("name",name);
            map.put("array",lsit);
            listall.add(map);
        }
        renderJsonRet(listall);
    }

    /**
     *
     *
     * @Author GuoShunYou
     * @Description 根据userid、化验单类型(0数据化验单、1图片化验单)查询日期对应的化验单列表
     * @date 上午 11:18   2018/10/30
     * @return void
     */
    public void selectLaborByTime(){
        TmLaboratoryInfo toc=(TmLaboratoryInfo)getReqBodyObject(TmLaboratoryInfo.class);
        //获取所有数据
        List<LaboratoryResponse> labres=TmLaboratoryInfo.dao.seleLaboraByTime(toc);
        //查询所有数据日期分组
        List<String> dateList= TmLaboratoryInfo.dao.getGroupByTime(toc);
        List listAll=new ArrayList<>();
        for(int i=0;i<dateList.size();i++){
            Map map=new HashMap();
            List listxiao=new ArrayList<>();

            map.put("time",dateList.get(i));
            for(int j=0;j<labres.size();j++){
                if(dateList.get(i).equals(labres.get(j).getCheckTime())){
                    Map mapx= new HashMap<>();
                    mapx.put("id",labres.get(j).getId());
                    mapx.put("createTime",labres.get(j).getCreateTime());
                    if("1".equals(toc.getLaboratoryType())){
                        mapx.put("path",labres.get(j).getPath());
                    }else{
                        mapx.put("name",labres.get(j).getLaboratoryNmae());
                    }
                    listxiao.add(mapx);
                }
            }

            if("1".equals(toc.getLaboratoryType())){
                List ss=new ArrayList<>();
                ss.add(listxiao);
                map.put("data",ss);
            }else{
                map.put("data",listxiao);
            }
            listAll.add(map);
        }
        renderJsonRet(listAll);
    }

    /**
     * @Author GuoShunYou
     * @Description 根据化验单订单id查询化验单详情数据
     * @date 下午 4:59   2018/10/30
     * @return void
     */
    public void selectIndsById(){
        TmLaboratoryInfo toc=(TmLaboratoryInfo)getReqBodyObject(TmLaboratoryInfo.class);
        List<IndicatorsIndicatorsResponse> list=TmLaboratoryInfoAttr.dao.getIndicatorsById(toc.getId());
        renderJsonRet(list);
    }

    /**
     * wy:分页查询数据化验单
     */
    public void findMethLaboratory(){
        String userId=getPara("userId");
        Map map = TmLaboratoryInfo.dao.findMethLaboratory(getQueryModel(),userId);
        renderPageJsonRet(map.get("count"),map.get("data"));
    }

    /**
     * wy:批量禁用数据化验单
     */
    public void delLaboratory(){
        String ids = getReqBodyString().replace("\"","").replace("\"","");
        if (TmLaboratoryInfo.dao.delLaboratory(ids)){
            renderJsonRet(500001);
        }else {
            renderJsonError(500002,"禁用");
        }
    }

    /**
     * wy:禁用数据化验单
     */
    public void delLabo(){
        TmLaboratoryInfo tli = (TmLaboratoryInfo) getReqBodyObject(TmLaboratoryInfo.class);
        if (tli.update()){
            renderJsonRet(500001);
        }else {
            renderJsonError(500002);
        }
    }
}
